package com.syq.courierDelivery.Listener;

import com.alibaba.fastjson.JSON;
import com.syq.courierDelivery.common.utils.PathPlanningUtils;
import com.syq.courierDelivery.common.utils.RedisUtils;
import com.syq.courierDelivery.entity.po.ExpressDelivery;
import com.syq.courierDelivery.entity.vo.MqReceiveVo;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class RabbitMqListener {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private PathPlanningUtils pathPlanningUtils;
    @RabbitListener(queues = "${epstation.queue}")
    public void myListener1(String message){
        System.out.println("消费者接收到的消息为：" + message);
        try {
            MqReceiveVo mqReceiveVo = JSON.parseObject(message, MqReceiveVo.class);
            Byte type = mqReceiveVo.getType();
            System.out.println("消费者接收到的消息类型为：" + type);
            switch (type) {
                //派件规划
                case 1:
                    List<ExpressDelivery> expressDeliveryList = new ArrayList<>();
                    if(mqReceiveVo.getDeliveryList().size() > 1) {
                        expressDeliveryList= pathPlanningUtils.pathPlan(mqReceiveVo);
                    } else {
                        expressDeliveryList = mqReceiveVo.getDeliveryList();
                    }

                    for(ExpressDelivery expressDelivery : expressDeliveryList) {
                        System.out.println(expressDelivery.getExpress().getAddress());
                    }
                    String key = "epstation:deliveryList:courierCode:" + mqReceiveVo.getCourierCode();
                    expressDeliveryList.stream().map(expressDelivery -> expressDelivery.getId()+",").forEach(System.out::print);
                    redisUtils.set(key,JSON.toJSONString(expressDeliveryList));
                    break;
                    //上门取件规划
                case 2:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
